{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**1类**\n",
    "\n",
    "Affiner:\n",
    ">包含的函数:\n",
    "param2mat(self, p):\n",
    "\n",
    "```python\n",
    "param2mat(self, p):\n",
    "sz = p.shape\n",
    "        q = np.zeros(sz)\n",
    "        # s, \\theta, r, \\phi\n",
    "        s, th, r, phi = p[2], p[3], p[4], p[5]\n",
    "        cth, sth, cph, sph = np.cos(th), np.sin(th), np.cos(phi), np.sin(phi)\n",
    "        ccc, ccs, css = cth * cph * cph, cth * cph * sph, cth * sph * sph\n",
    "        scc, scs, sss = sth * cph * cph, sth * cph * sph, sth * sph * sph\n",
    "        # 这几个数分别是什么含义?\n",
    "        q[0], q[1] = p[0], p[1]\n",
    "        q[2] = s * (ccc + scs + r * (css - scs))\n",
    "        q[3] = s * (r * (ccs - scc) - ccs - sss)\n",
    "        q[4] = s * (scc - ccs + r * (ccs + sss))\n",
    "        q[5] = s * (r * (ccc + scs) - scs + css)\n",
    "        return q\n",
    "```\n",
    "首先输入的是一个[1, 6]的向量p,p[0],p[1]表示的目标框的中心点的位置,那p[2], p[3], p[4], p[5]分别代表什么呢?\n",
    "\n",
    "首先要弄清楚这个函数的作用.\n",
    "\n",
    "输入变量:$s,\\theta,r,\\phi$\n",
    "\n",
    "输出q:\n",
    "$$\n",
    "\\left\\{\n",
    "\\begin{matrix}\n",
    "q[0]=p[0]\\\\\n",
    "q[1]=p[1]\\\\\n",
    "q[2]=p[2] \\times (cos(p[3]) \\times cos(p[5]) \\times cos(p[5]) + sin(p[3]) \\times cos(p[5]) \\times sin(p[5]) + p[4] \\times (cos(p[3]) \\times sin(p[5]) \\times sin(p[5]) - sin(p[3]) \\times cos(p[5]) \\times sin(p[5]))\\\\\n",
    "4\\\\\n",
    "\\end{matrix}\n",
    "\\right.\n",
    "$$\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ">def param2geom(self, p):\n",
    "\n",
    "```python\n",
    "def param2geom(self, p):\n",
    "    sz = p.shape\n",
    "    q = np.zeros(sz)\n",
    "    A = np.array([[p[2], p[3]],\n",
    "                  [p[4], p[5]]])\n",
    "\n",
    "    U, sigma, VT = svd(A)\n",
    "\n",
    "    V = VT.T\n",
    "    S = np.zeros((len(sigma), len(sigma)))\n",
    "    for i in range(len(sigma)):\n",
    "        S[i][i] = sigma[i]\n",
    "\n",
    "    if det(U) < 0:\n",
    "        U = U[:, 2::-1]\n",
    "        S = S[2::-1, 2::-1]\n",
    "        V = V[:, 2::-1]\n",
    "\n",
    "    phi = np.arctan2(V[0, 1], V[0, 0])\n",
    "    if phi <= -np.pi / 2:\n",
    "        c, s = np.cos(-np.pi / 2), np.sin(-np.pi / 2)\n",
    "\n",
    "        R = np.array([[c, -s],\n",
    "                      [s, c]])\n",
    "        V = np.dot(V, R)\n",
    "        S = np.dot(np.dot(R.T, S), R)\n",
    "    if phi >= np.pi / 2:\n",
    "        c, s = np.cos(np.pi / 2), np.sin(np.pi / 2)\n",
    "\n",
    "        R = np.array([[c, -s],\n",
    "                      [s, c]])\n",
    "        V = np.dot(V, R)\n",
    "        S = np.dot(np.dot(R.T, S), R)\n",
    "\n",
    "    q[0], q[1] = p[0], p[1]\n",
    "    q[2] = S[0, 0]\n",
    "    q[3] = np.arctan2(np.dot(U[1, 0], V[0, 0]) + np.dot(U[1, 1], V[0, 1]),\n",
    "                      np.dot(U[0, 0], V[0, 0]) + np.dot(U[0, 1], V[0, 1]))\n",
    "    q[4] = S[1, 1] / S[0, 0]\n",
    "    q[5] = np.arctan2(V[0, 1], V[0, 0])\n",
    "\n",
    "    return q\n",
    "\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
